package com.chenliang.nacos;

import com.chenliang.ribbon.RibbonRandomConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @program: springcloud_all
 * @description:
 * @author: 陈亮
 * @create: 2022-04-03 14:55
 */
@SpringBootApplication
@EnableDiscoveryClient   //表明这个服务被nacos注册中心管理
@RibbonClients(value = {
        @RibbonClient(name = "nacos-8082", configuration = RibbonRandomConfig.class) // 消费者在调用生产者的时候,给那个生产者在被调用的时候,进行负载均衡策略呢,在这里设置
})
@EnableFeignClients
public class Nacos8083Application {


    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Nacos8083Application.class, args);
        String username = context.getEnvironment().getProperty("username");
        System.out.println(username);
    }


    /**
     * nacos是实现客户端负载均衡的,但是naocs自己是实现不了这个功能的,还是有ribbon实现的
     * 默认客户端负载均衡策略是轮询,如果需要修改策略需要自己在配置类进行配置
     *
     * @param builder
     * @return
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }
}
